#include "StdAfx.h"
#include "ReaderWriter.h"

template <class Name, class Name2>
inline color_writer<Name, Name2>
make_color_writer(Name n, Name2 n2) {
	return color_writer<Name, Name2 > (n, n2);
}

bool ReaderWriter::graph_read(Graph& graph) {
	std::cout << "Print path to .dot file with graph for coloring: \n";
	std::string input;
	std::cin >> input;
	std::ifstream file(input.c_str());
	if (!file) {
		std::cerr << "Error on opening file \n";
		file.close();
		return false;
	}
	property_map<Graph, boost::vertex_name_t>::type name =
		get(vertex_name, graph);
	dynamic_properties dp;
	dp.property("node_id", name);

	property_map<Graph, boost::vertex_color_t>::type mass =
		get(vertex_color, graph);
	dp.property("color", mass);
	std::cout << "Reading graph from file... ";
	read_graphviz(file, graph, dp, "node_id");
	file.close();
	return true;
}

bool ReaderWriter::graph_write (const Graph& graph){
	int size = boost::num_vertices(graph);

	std::vector<std::size_t> colors (size);
	std::vector<std::string> names ( size);

	boost::graph_traits<Graph>::vertex_iterator vi, vi_end;
	for (boost::tie(vi, vi_end) = boost::vertices(graph); vi != vi_end; ++vi) {
		colors[*vi] = get(vertex_color, graph)[*vi];
		names[*vi] = get(vertex_name, graph)[*vi];
	}

	std::cout << "Enter dot file name for output graph: \n";
	std::string output("output.dot");
	std::cin >> output;
	std::cout << "Writing to file...";
	std::fstream ofile;
	ofile.open(output.c_str(), std::ios::out);
	boost::write_graphviz(ofile, graph, make_color_writer(colors, names));
	ofile.close();

	std::cout << " OK\n";
	return true;
}
